Skip to content

Make data.Const comparable with list and dict objects compatible with the layout #1420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

whitequark
Copy link
Member

Before this commit, data.Const was only comparable to data.Const or data.View.

After this commit, in addition, it is also comparable to dict or list provided that such a value is accepted by layout.const() where layout is the layout of the data.Const object.

This change greatly reduces boilerplate in tests by enabling e.g.:

assert (await stream_get(ctx, stream)) == {"value": 1}

instead of:

assert (await stream_get(ctx, stream)) == Const({"value": 1}, stream.p.shape())

Note that, unlike Layout.const, which accepts arbitrary Mapping or Sequence objects, only dict and list are accepted in comparisons. Also, data.View continues to be comparable only to data.View and data.Const. This is to minimize the scope of the change and reduce likelihood of undesirable side effects when backported to the 0.5.x branch.

Fixes #1414.

Previously, `KeyError` was raised, which should not be escaping
the implementation (`Layout.const` is not a retrieval method).
Before this commit, `data.Const` was only comparable to `data.Const` or
`data.View`.

After this commit, in addition, it is also comparable to `dict` or
`list` provided that such a value is accepted by `layout.const()` where
`layout` is the layout of the `data.Const` object.

This change greatly reduces boilerplate in tests by enabling e.g.:

    assert (await stream_get(ctx, stream)) == {"value": 1}

instead of:

    assert (await stream_get(ctx, stream)) == Const({"value": 1}, stream.p.shape())

Note that, unlike `Layout.const`, which accepts arbitrary `Mapping` or
`Sequence` objects, only `dict` and `list` are accepted in comparisons.
Also, `data.View` continues to be comparable only to `data.View` and
`data.Const`. This is to minimize the scope of the change and reduce
likelihood of undesirable side effects when backported to the 0.5.x
branch.

Fixes amaranth-lang#1414.
@whitequark
Copy link
Member Author

For additional context, the main reason View comparisons are unimplemented are this code:

end = Signal()
with m.If(stream.payload == {"data": 1, "end": end}):
    ...

Whether it should be allowed or not is definitely RFC-worthy, and @wanda-phi actually had a planned RFC that would bring the underlying functionality for composing a View out of a concatenation of parts. For the time being, I think it's simplest and safest to not touch View at all until we're ready.

@whitequark whitequark added this pull request to the merge queue Jun 27, 2024
@whitequark whitequark added this to the 0.6 milestone Jun 27, 2024
Merged via the queue into amaranth-lang:main with commit 42d90a3 Jun 27, 2024
20 checks passed
@whitequark whitequark deleted the data-const-compare-dict-list branch June 27, 2024 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Allow comparison of data.Const with dict and list
2 participants